/*
 * waitq example
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mm.h>

#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/kthread.h>


DECLARE_WAIT_QUEUE_HEAD(my_waitq);
static int condition;

static struct task_struct *tsk;

static int my_task_fn(void *data)
{
	pr_info("%s: prepare to wait the event...\n", __func__);
	wait_event(my_waitq, condition == 1);  //not &my_waitq, ugly
	pr_info("%s: event arrived, start to work...\n", __func__);

	return 0;
}

static int __init waitq_init(void)
{
	pr_info("%s...\n", __func__);

	tsk = kthread_run(my_task_fn, NULL, "mytask");
	if (IS_ERR(tsk)) {
		pr_err("ktread_run failed\n");
		return PTR_ERR(tsk);
	}

	msleep(500);
	condition = 1;
	wake_up(&my_waitq);

	return 0;
}

static void __exit waitq_exit(void)
{
	pr_info("%s...\n", __func__);
}

module_init(waitq_init);
module_exit(waitq_exit);
MODULE_LICENSE("GPL");
